/**
 * Created by fantaros on 16/1/8.
 */

(function (window, undefined){
    window.rending = function () {
        var cx = 0, cy = 0, lx, ly, document = window.document,
            canvas = document.getElementById("main"),
            ctx = canvas.getContext("2d"),
            data = window.datasource.tmpdata,
            firstdata = data[0], price, maxabs = 0, psp, cp,
            spacegap;
        canvas.backgroundColor = "black";
        price = +(firstdata["lastprice"] || 0);
        psp = firstdata["presettlementprice"] || 0;
        for (var i = 0; i < data.length; ++i) {
            cp = +(data[i]["lastprice"]);
            if (Math.abs(cp - psp) > maxabs) {
                maxabs = Math.abs(cp - psp);
            }
        }
        cy = (price - psp + maxabs) / (2 * maxabs) * canvas.height;
        spacegap = canvas.width / (data.length + 1);
        ctx.strokeStyle = "blue";
        ctx.lineWidth = 0.5;
        ctx.strokeLinecap = 0;
        ctx.strokeWidth = 0.5;
        ctx.save();
        //ctx.moveTo(cx, cy);
        lx = cx;
        ly = cy;
        for (var i = 1; i < data.length; ++i) {
            cp = +(data[i]["lastprice"]);
            cx = i * spacegap;
            cy = (cp - psp + maxabs) / (2 * maxabs) * canvas.height;
            ctx.beginPath();
            ctx.moveTo(lx, ly);
            ctx.lineTo(cx, cy);
            ctx.closePath();
            ctx.stroke();
            lx = cx;
            ly = cy;
        }
        //ctx.stroke();
        ctx.restore();
    };
})(window);